<?php
/**
 * Monitor 各种上报
 *
 * @author		黑冰 <954650386@qq.com>
 */

use \waf\Waf;
use \waf\log\WLog;
use \waf\controller\WController;

class MonitorController extends WController
{
    /**
     * 统计接口调用
     */
    public function interfaceStatisticsAction()
    {
        WLog::logInfo("in.", __FILE__, __LINE__, __FUNCTION__);

        $server_name    = trim($this->getRequest()->getParam('server_name',         ''));
        $module_name    = trim($this->getRequest()->getParam('module_name',         ''));
        $interface_name = trim($this->getRequest()->getParam('interface_name',      ''));
        $cost_time      = intval($this->getRequest()->getParam('cost_time',         0));
        $success        = intval($this->getRequest()->getParam('success',           0));
        WLog::logDebug("server_name=$server_name, module_name=$module_name, interface_name=$interface_name, cost_time=$cost_time, success=$success", __FILE__, __LINE__, __FUNCTION__);

        if($server_name && $module_name && $interface_name)
        {
            $key   = Waf::getConfig('key', 'waf_interface_statistics');
            $ikey    = "$server_name|$module_name|$interface_name";

            // 一级key
            $arrGSData = Waf::WData()->getData($key);
            if(!isset($arrGSData))
            {
                $arrGSData  = array();
            }

            // 二级key
            if(!isset($arrGSData[$ikey]))
            {
                $arrGSData[$ikey]   = array();
                $arrGSData[$ikey]['sct']    = 0;
                $arrGSData[$ikey]['sc']     = 0;
                $arrGSData[$ikey]['fct']    = 0;
                $arrGSData[$ikey]['fc']     = 0;
            }

            // 成功
            if($success)
            {
                $arrGSData[$ikey]['sct']    += $cost_time;
                $arrGSData[$ikey]['sc']     += 1;
            }else{
                $arrGSData[$ikey]['fct']    += $cost_time;
                $arrGSData[$ikey]['fc']     += 1;
            }

            // 存储数据
            Waf::WData()->setData($key, $arrGSData);
        }

        $this->output();
    }

    /**
     * 统计调用次数
     */
    public function countStatisticsAction()
    {
        WLog::logInfo("in.", __FILE__, __LINE__, __FUNCTION__);

        $server_name    = trim($this->getRequest()->getParam('server_name',         ''));
        $module_name    = trim($this->getRequest()->getParam('module_name',         ''));
        $interface_name = trim($this->getRequest()->getParam('interface_name',      ''));
        $count          = floatval($this->getRequest()->getParam('count',         0));

        WLog::logDebug("server_name=$server_name, module_name=$module_name, interface_name=$interface_name, count=$count", __FILE__, __LINE__, __FUNCTION__);

        if($server_name && $module_name && $interface_name)
        {
            $key   = Waf::getConfig('key', 'waf_count_statistics');
            $ikey    = "$server_name|$module_name|$interface_name";

            // 一级key
            $arrGSData = Waf::WData()->getData($key);
            if(!isset($arrGSData))
            {
                $arrGSData  = array();
            }

            // 二级key
            if(!isset($arrGSData[$ikey]))
            {
                $arrGSData[$ikey] = 0;
            }

            $arrGSData[$ikey] += $count;

            // 存储数据
            Waf::WData()->setData($key, $arrGSData);
        }

        $this->output();
    }
}